import datetime
from project.models import Project, Participate, ProjectRole, ProjectEmpower, ProjectPermission
from user.models import User


def create_project(name, user, leader_id=None, top_designer_id=None, date=None, current_version='', detail=''):
    leader = user if leader_id is None else User.objects.get(id=leader_id)
    top_designer = user if top_designer_id is None else User.objects.get(id=top_designer_id)
    start_time = datetime.date.today() if date is None else \
        datetime.date(date.get('year'), date.get('month'), date.get('day'))
    new_project = Project(name=name, leader=leader, start_time=start_time,
                          current_version=current_version, detail=detail)
    new_role = ProjectRole(pid=new_project, name='总设计师')
    new_empower = ProjectEmpower(rid=new_role, pid=ProjectPermission.objects.get(id=1))  # 1-所有权限
    new_participate = Participate(uid=top_designer, rid=new_role)
    new_role2 = ProjectRole(pid=new_project, name='开发者')
    new_empower2 = ProjectEmpower(rid=new_role2, pid=ProjectPermission.objects.get(id=2))
    new_project.save()
    new_role.save()
    new_role2.save()
    new_empower.save()
    new_empower2.save()
    new_participate.save()
    return new_project
